package vn.gq.udv.utils;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

public class GooglePlayButton {
	
	public static final String STORE_UNGDUNGVIET_URI = "pub:UNGDUNGVIET";
	public static final String STORE_LOCXOAY_URI = "pub:LOCXOAY";
	
	public enum GooglePlayAccount {
		UNGDUNGVIET, LOCXOAY
	}
	
	private Context mContext;
	
	public GooglePlayButton(Context context) {
		mContext = context;
	}
	
	public static GooglePlayButton newInstance(Context context) {
		GooglePlayButton f = new GooglePlayButton(context);
		return f;
	}
	
	public void loadListApp(GooglePlayAccount account) {
		switch (account) {
		case LOCXOAY:
			startGooglePlayActivity(STORE_LOCXOAY_URI);
			break;
		default:
			startGooglePlayActivity(STORE_UNGDUNGVIET_URI);
		}
	}
	
	
	public void startGooglePlayActivity(String uri) {
		try {
			mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("market://search?q=%s", uri))));
		} catch(ActivityNotFoundException e) {
			String httpUri = String.format("https://play.google.com/store/search?q=%s&c=apps&sort=1", uri);
			mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(httpUri)));
		}
	}
	
	
	public void startGooglePlayForDetail() {
		String packageName = mContext.getPackageName();
		try {
			mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("market://details?id=%s", packageName))));
		} catch(ActivityNotFoundException e) {
			Toast.makeText(mContext, "Ứng dụng Google Play không được cài trên máy", Toast.LENGTH_SHORT).show();
		}
	}
	
}
